package local.sumw3.findmydevices;

import local.sumw3.findmydeivces.R;
import local.sumw3.findmydevices.utils.SimpleCrypto;
import local.sumw3.findmydevices.utils.UIUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {

    private EditText mEtUsername = null;

    private EditText mEtPassword = null;

    private String imei = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        imei = UIUtils.getIMIE(this);

        mEtUsername = (EditText) findViewById(R.id.userName);
        mEtPassword = (EditText) findViewById(R.id.userPassword);

        findViewById(R.id.loginBtn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mEtUsername.getText() == null || "".equals(mEtUsername.getText().toString())) {
                    mEtUsername.setError(getString(R.string.inputerror_username));
                    return;
                }
                if (mEtPassword.getText() == null || "".equals(mEtPassword.getText().toString())) {
                    mEtPassword.setError(getString(R.string.inputerror_password));
                    return;
                }
                mEtUsername.setError(null);
                mEtPassword.setError(null);
                String username = mEtUsername.getText().toString();
                String password = mEtPassword.getText().toString();

                try {
                    String encryptedPw = SimpleCrypto.encrypt(imei, password);
                    SharedPreferences userPrefs = getSharedPreferences(UIConstants.PREFS_NAME, 0);
                    SharedPreferences.Editor editor = userPrefs.edit();
                    editor.putString(UIConstants.PA_LOGIN_USERNAME, username);
                    editor.putString(UIConstants.PA_LOGIN_USERPASSWORD, encryptedPw);
                    editor.commit();
                } catch (Exception e) {
                    Log.e(this.getClass().toString(), e.getMessage());
                }

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(UIConstants.PA_LOGIN_USERNAME, username);
                bundle.putString(UIConstants.PA_LOGIN_USERPASSWORD, password);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        ((TextView) findViewById(R.id.mineDesTxt)).setMovementMethod(LinkMovementMethod.getInstance());

        SharedPreferences userPrefs = getSharedPreferences(UIConstants.PREFS_NAME, 0);
        String lstUsername = userPrefs.getString(UIConstants.PA_LOGIN_USERNAME, null);
        String lstPassword = userPrefs.getString(UIConstants.PA_LOGIN_USERPASSWORD, null);
        if (lstUsername != null && !"".equals(lstUsername) && lstPassword != null && !"".equals(lstPassword)) {
            try {
                lstPassword = SimpleCrypto.decrypt(imei, lstPassword);
                mEtUsername.setText(lstUsername);
                mEtPassword.setText(lstPassword);
            } catch (Exception e) {
                Log.e(this.getClass().toString(), e.getMessage());
            }
        }
    }
}
